<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.9
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############


//$allFiles = JFX_Data::getAllModuleFiles($this->keyname);
//var_dump($allFiles);

//var_dump($_GET);
//return;

if(post('folder')!=''){
	var_dump($_POST);
	var_dump($_FILES);
	return;
}

if(get('filepath')!='' && get('filename')!=''){
	// want to set a new file to this block
	$file = JFX_Data::getFile(get('filepath'), get('filename'), $this->keyname);

	if($file instanceof JFX_Data_File){
		$block->setConfig('filepath', $file->meta['filepath']);
		$block->setConfig('filename', $file->meta['filename']);
	}
	
}



$tree = $this->getFolderHeirarchy();

$res = '<ul id="jfx-imager-filepicker" class="filepicker treeview">'.imager_filepicker_add_elements_to_list($tree, array(), $block).'</ul>';

echo $res;

$js = <<<JS
<script type="text/javascript" language="JavaScript">
$('#jfx-imager-filepicker').treeview({
persist: "location",
			collapsed: true,
			unique: true
});
</script>
JS;

echo $js;


echo '<img src="'.$this->imageUrl.'/serve.php?maxwidth=400&amp;maxheight=400&amp;absolutepath='.urlencode($block->getAbsolutePath()).'" />';



function imager_filepicker_add_elements_to_list($list, $prefix=array(), $block){
	$CONFIG = JFX::registry('config');
	$module = JFX_Module::getInstance('imager');
	$retval = '';
	$blockFilepath = @$block->getAbsolutePath();
	foreach($list as $k=>$row){
		
		$retval .= '<li>'.$row['title'];
		if(is_array($row['subfolders']) && count($row['subfolders'])>0){
			$prefix[] = $row['id'];
			$retval .= '<ul>'.imager_filepicker_add_elements_to_list($row['subfolders'], 
				$prefix,
				$block).'</ul>';
		}
		
		if(is_array($row['files']) && count($row['files'])>0){
			$retval .= '<ul>';
			foreach($row['files'] as $fk=>$frow){
				if($frow->getAbsolutePath()==$blockFilepath){
					// file belongs to block
					$editButton = '<img src="'.$CONFIG->imageUrl.'/admin/icons/tick-green.png'.'" alt="Current image!" />';
				}else{
					// file doesn't belong to block

					$button1 = new JFX_Block_Button;
					$button1->pageId = $block->pageid;
					$button1->alternativeText = $module->lang('block_edit_button_alt');
					$button1->moduleName = $module->keyname;
					$button1->moduleMethod = 'updateSettings';
					$button1->imageUrl = $module->imageUrl.'/camera-silver.png';
					$button1->title = $module->lang('block_edit_button_title');
					$button1->blockId = $block->id;
					$button1->filepath = $module->getFolderPath($row['id']);
					$button1->filename = $frow->meta['filename'];
					$button1->rebuildPage = true;

					$buttonJson = str_replace('"', "'", $button1->__toJson());

					$editFunction = <<<JS
AdminBlocks.tabber.loadButtonContent({$buttonJson});

JS;

					$editButton = '<a href="javascript: void(null);" onclick="'.$editFunction.'">
						<img src="'.$CONFIG->imageUrl.'/admin/icons/delete.png" alt="Not selected" />
					</a>';
				}

				$preview = '<img src="'.$module->imageUrl.'/serve.php?maxwidth=80&amp;maxheight=80&amp;filename='.$frow->meta['filename'].'&amp;filepath='.$module->getFolderPath($row['id']).'" />';

				$retval .= '<li>'.$preview.$frow->meta['friendly_name'].$editButton.'</li>';


			}
			$retval .= '</ul>';
		}

		$retval .= '</li>';
	}
	return $retval;
}




